package v1

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// @Tags SysTag
// @Summary 创建SysTag
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysTag true "创建SysTag"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysTag/createSysTag [post]
func CreateSysTag(c *gin.Context) {
	var sysTag model.SysTag
	_ = c.ShouldBindJSON(&sysTag)
	//验证前台数据合法性
	if err := utils.Verify(sysTag, utils.SysTagVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 唯一性校验
	_, tag := service.GetSysTag(0, sysTag.TagName)
	if tag.ID > 0 {
		response.FailWithMessage("标签名称已存在", c)
		return
	}
	// 赋值操作用户
	_, user := service.FindUserById(utils.GetUserIdByRequest(*c))
	sysTag.TagStatus = "未审核"
	sysTag.TagAuthor = user.Username + "(" + user.NickName + ")"
	// 创建标签
	if err := service.CreateSysTag(sysTag); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags SysTag
// @Summary 删除SysTag
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysTag true "删除SysTag"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sysTag/deleteSysTag [delete]
func DeleteSysTag(c *gin.Context) {
	var sysTag model.SysTag
	_ = c.ShouldBindJSON(&sysTag)
	if err := service.DeleteSysTag(sysTag); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags SysTag
// @Summary 批量删除SysTag
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除SysTag"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /sysTag/deleteSysTagByIds [delete]
func DeleteSysTagByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteSysTagByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags SysTag
// @Summary 更新SysTag
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysTag true "更新SysTag"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sysTag/updateSysTag [put]
func UpdateSysTag(c *gin.Context) {
	var sysTag model.SysTag
	_ = c.ShouldBindJSON(&sysTag)
	if err := service.UpdateSysTag(sysTag); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags SysTag
// @Summary 用id查询SysTag
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysTag true "用id查询SysTag"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sysTag/findSysTag [get]
func FindSysTag(c *gin.Context) {
	var sysTag model.SysTag
	_ = c.ShouldBindQuery(&sysTag)
	if err, resysTag := service.GetSysTag(sysTag.ID, sysTag.TagName); err != nil {
		if err.Error() == "record not found" {
			global.GVA_LOG.Info("查询记录不存在!", zap.Any("err", err))
			response.OkWithData("记录不存在", c)
		} else {
			global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
			response.FailWithMessage("查询失败", c)
		}

	} else {
		response.OkWithData(gin.H{"resysTag": resysTag}, c)
	}
}

// @Tags SysTag
// @Summary 分页获取SysTag列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysTagSearch true "分页获取SysTag列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysTag/getSysTagList [get]
func GetSysTagList(c *gin.Context) {
	var pageInfo request.SysTagSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetSysTagInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// 查询所有的已审核的标签
func GetSysTags(c *gin.Context) {
	if err, list := service.GetSysTags(); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List: list,
		}, "获取成功", c)
	}
}
